package com.sfac.day02.modules.test.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sfac.day02.modules.common.entity.ResultBean;
import com.sfac.day02.modules.common.entity.SearchBean;
import com.sfac.day02.modules.test.dao.CityDao;
import com.sfac.day02.modules.test.entity.City;
import com.sfac.day02.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.html.Option;
import java.util.Collections;
import java.util.Optional;

@Service
public class CityServiceImp implements CityService {
    @Autowired
    private CityDao cityDao;
    //增加city
    @Override
    @Transactional
    public ResultBean<City> addCity(City city) {
        cityDao.addCity(city);
        return new ResultBean<City>(ResultBean.ResultStatus.SUCCESS.code,"success",city);
    }
    //修改city
    @Override
    public ResultBean<City> updateCity(City city) {
        cityDao.updateCity(city);
        return new ResultBean<City>(ResultBean.ResultStatus.SUCCESS.code, "Update city success",city);
    }
    //拖过id删除city
    @Override
    public ResultBean<City> deleteCityById(Integer cityId) {
        cityDao.deleteCityById(cityId);
        return new ResultBean<City>(ResultBean.ResultStatus.SUCCESS.code, "Delete city success");
    }
    //通过id查看city
    @Override
    public ResultBean<City> selectCityByCityId(Integer cityId) {
        return new ResultBean<City>(ResultBean.ResultStatus.SUCCESS.code, "query success",
                cityDao.selectCityByCityId(cityId));
    }

   /* @Override
    public ResultBean<City> getCityByCityId(int cityId) {
        return new  ResultBean<City>(ResultBean.ResultStatus.SUCCESS.code,"sucess",
                cityDao.getCityByCityId(cityId));
    }*/

    @Override
    public PageInfo<City> getgetCitiesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getPageNumber(),searchBean.getPageSize());
        return new PageInfo<City>(Optional.ofNullable(cityDao.getgetCitiesBySearchBean(searchBean))
                        .orElse(Collections.emptyList()));
    }
    //联表查询接口

}
